package com.fqdf.framework.android.activity;

import android.support.annotation.UiThread;
import android.support.v4.widget.NestedScrollView;
import android.support.v4.widget.SwipeRefreshLayout;

import com.fqdf.framework.android.adapter.AdapterSwipListener;
import com.fqdf.framework.android.adapter.BaseMyAdapter;
import com.fqdf.framework.android.net.response.BasePageInfoBean;
import com.fqdf.framework.android.util.CheckLogicUtil;
import com.fqdf.framework.android.util.LogUtil;
import com.fqdf.framework.android.view.LoadMoreListView;
import com.fqdf.framework.android.view.TwoSwipeRefreshLayout;
import com.fqdf.framework.android.view.listener.LoadMoreListener;
import com.fqdf.framework.android.R;

import java.util.List;

public abstract class BaseSwipLoadMoreActivity extends BaseTitleActivity implements SwipeRefreshLayout.OnRefreshListener
        , LoadMoreListener {

    private LoadMoreListView mListView;
    private TwoSwipeRefreshLayout mSwipeRefreshLayout;

    /**
     * 初始化加载更多的view数据
     *
     * @param listView           listview
     * @param swipeRefreshLayout 下拉刷新view
     * @param noDataLayout       空数据显示view
     */
    protected void initSwipLoadMoreView(LoadMoreListView listView, TwoSwipeRefreshLayout swipeRefreshLayout, NestedScrollView noDataLayout) {
        mListView = listView;
        mSwipeRefreshLayout = swipeRefreshLayout;
        listView.setLoadMoreListener(this);
        mSwipeRefreshLayout.setSwipeableChildren(listView, noDataLayout);
        mSwipeRefreshLayout.setColorSchemeResources(R.color.a);
        mSwipeRefreshLayout.setOnRefreshListener(this);
    }

    /**
     * 开始处理刷新
     */
    protected void refreshDataAnim() {
        if (mSwipeRefreshLayout == null) {
            return;
        }

        mSwipeRefreshLayout.post(new Runnable() {
            @Override
            public void run() {
                mSwipeRefreshLayout.setRefreshing(true);
                onRefresh();
            }
        });
    }

    @UiThread
    protected abstract void loadMoreData();

    @Override
    public void autoLoadMore() {
        if (!mSwipeRefreshLayout.isRefreshing()) {
            mSwipeRefreshLayout.setRefreshing(true);
            loadMoreData();
        }
    }

    /**
     * 处理加载更多的逻辑
     *
     * @param curPage  当前显示的页
     * @param pageNum  总共的分页
     * @param dataList 处理数据
     */
    protected void handleLoadMore(int curPage, int pageNum, List<?> dataList) {
        mListView.setCanLoadMore(curPage != pageNum);

        BaseMyAdapter adapter = (BaseMyAdapter) mListView.getAdapter();

        if (pageNum == 1) {
            if (!CheckLogicUtil.isEmpty(dataList)) {
                adapter.setList(dataList);
                adapter.notifyDataSetChanged();
                mSwipeRefreshLayout.showOriginData();
            } else {
                mSwipeRefreshLayout.showNoData();
            }
        } else {
            if (!CheckLogicUtil.isEmpty(dataList)) {
                adapter.addList(dataList);
                adapter.notifyDataSetChanged();
            }
        }
    }

    /**
     * 更新加载更多以及更新数据
     *
     * @param bean 请求结果返回的basepagebean 用来获取页数属性 并且数据bean对象需要实现AdapterSwipListener接口才可以更新数据
     */
    protected void handleLoadMore(BasePageInfoBean bean) {

        if (bean == null) {
            return;
        }
        if (bean instanceof AdapterSwipListener) {
            handleLoadMore(bean.getPages(), bean.getPageNum(), ((AdapterSwipListener) bean).getAdapterList());
        } else {
            LogUtil.e(BaseSwipLoadMoreActivity.class, "not get data to update adapter");
        }
    }

    /**
     * 处理加载更多以及更新数据
     *
     * @param bean  请求结果返回的basepagebean 用来获取页数属性
     * @param datas 需要更新的数据，由于获取的是父类的基本数据，所以为了封装需要单独传递数据，没有再次封装。
     */
    protected void handleLoadMore(BasePageInfoBean bean, List<?> datas) {

        if (bean == null) {
            return;
        }
        handleLoadMore(bean.getPages(), bean.getPageNum(), datas);
    }

    /**
     * 关闭动画
     */
    public void closeSwipRefreshAnim() {
        mSwipeRefreshLayout.postDelayed(new Runnable() {
            @Override
            public void run() {
                mSwipeRefreshLayout.setRefreshing(false);
            }
        }, 500);

    }
}
